//
//  BLMYTagController.m
//  Byelone
//
//  Created by Qi Liu on 2021/2/8.
//

#import "BLMYTagController.h"
#import "BLTagViewCell.h"


@interface BLMYTagController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic,strong) NSMutableDictionary * tagDic;
@end

@implementation BLMYTagController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tagDic = [NSMutableDictionary dictionary];
    
    self.title = @"编辑标签";

    _dataArray = [BLInfoStyleInfo mj_objectArrayWithKeyValuesArray:[BLUserManager shareUser].commonInfo.styleMap];

    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, BL_WINDOW_WIDTH, (BL_WINDOW_HEIGHT - BL_NAVANDSTATUS_HEIGHT)) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    
    [_myTableView registerClass:[BLTagViewCell class] forCellReuseIdentifier:NSStringFromClass(BLTagViewCell.class)];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BLTagViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BLTagViewCell.class)];
    if (cell == nil) {
        cell = [[BLTagViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BLTagViewCell"];
    }
    
    BLInfoStyleInfo * item =  [_dataArray objectAtIndex:indexPath.section];
    cell.titleLabel.text = item.name;
    
    __block NSMutableArray * array = [NSMutableArray array];
    [item.styles enumerateObjectsUsingBlock:^(BLInfoStyleItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BLInfoStyleItem * styleItem = (BLInfoStyleItem *)obj;
        [array addObject:styleItem.styleName];
    }];
    cell.index = indexPath;
    [cell setTextArray:array row:indexPath.section];
    
    MJWeakSelf;
    cell.cellBlock = ^(NSIndexPath *index, NSArray *itemArray) {
        NSLog(@"index.section:%ld 数组:%@",(long)index.section,itemArray);
        
        BLInfoStyleInfo * styleInfo =   [weakSelf.dataArray objectAtIndex:index.section];
        
        [self handelSection:index.section TagArray:styleInfo.styles andIndexs:itemArray];
        
     
        
    };
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    BLInfoStyleInfo * item =  [_dataArray objectAtIndex:indexPath.section];
    __block NSMutableArray * array = [NSMutableArray array];
    [item.styles enumerateObjectsUsingBlock:^(BLInfoStyleItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        BLInfoStyleItem * styleItem = (BLInfoStyleItem *)obj;
        [array addObject:styleItem.styleName];
    }];
    
    return [BLTagViewCell cellHeightTextArray:array Row:indexPath.section];
}

/// 处理返回 的tag
/// @param section section
/// @param ary section 对应的tagarray
/// @param indexs 这个数组里包含的是对应 tagarray 的index 值
-(void)handelSection:(NSInteger)section TagArray:(NSArray *)ary andIndexs:(NSArray *)indexs {
    
    
    NSMutableArray * array = [NSMutableArray array];
    for (int i = 0; i<indexs.count; i++) {
        NSInteger index = [[indexs objectAtIndex:i] integerValue];
        BLInfoStyleItem * item = [ary objectAtIndex:index];
        [array addObject:item];
    }
    
    NSString * sectionKey = [NSString stringWithFormat:@"%ld",section];
    
    [_tagDic setValue:[array copy] forKey:sectionKey];
   
    
   __block NSMutableArray * idArray = [NSMutableArray array];
   __block NSMutableArray * nameArray = [NSMutableArray array];
    
    [_tagDic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        NSArray * tarray = [NSArray arrayWithArray:obj];
        if (tarray.count > 0) {
            for (int i = 0; i<tarray.count; i++) {
                BLInfoStyleItem * taItem = [tarray objectAtIndex:i];
                [idArray addObject:taItem.styleId];
                [nameArray addObject:taItem.styleName];
            }
        }
    }];
    
    self.tagIdArray = idArray;
    self.tagNameArray = nameArray;
//    NSLog(@"tag的值----%@",finArray);

}

-(void)leftAction{
    if (_tag) {
        _tag(self.tagIdArray,self.tagNameArray);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSMutableArray *)tagNameArray{
    if (!_tagNameArray) {
        _tagNameArray = [NSMutableArray array];
    }
    return _tagNameArray;
}
- (NSMutableArray *)tagIdArray{
    if (!_tagIdArray) {
        _tagIdArray = [NSMutableArray array];
    }
    return _tagIdArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
